package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.mapper.StudentMapper;
import org.example.pojo.Student;

import java.io.IOException;
import java.io.InputStream;
//修改静态字段
public class MyBatisDemo8 {
    public static void main(String[] args) throws IOException {
        //加载mybatis核心配置文件 获取sqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取sqlSession对象 用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setStudentid(6);
        student.setRealname("李李");
        int count = studentMapper.update(student);
        System.out.println(count);//获取添加成功的id
        //提交事务
        sqlSession.commit();
        //断开连接
        sqlSession.close();
    }
}
